<?php
/*
	本插件：$this->model('member')->方法名();
	其他地方调用：model('jy_scrm/member')->方法名();
*/
namespace jy_scrm;
defined('BY_JYA') or exit('error');
class member extends \plugin{
	public function batch($external_userid,$limit=100,$cursor='',$isnew=false){
		if(!$external_userid){
			exi('请传入员工ID','error');
		}
		$content=model('core/wxwork_userapi')->get_batch_member($external_userid,$cursor,$limit);
		foreach($content['external_contact_list'] as $item){
			$id=pdo_getcolumn('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'external_userid'=>$item['external_contact']['external_userid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($id && $isnew){
				continue;
			}
			$item=array_merge($item,$item['external_contact']);
			$item['id']=$id;
			$this->create($item);
		}
		if($content['next_cursor']){
			$this->batch($external_userid,$limit,$content['next_cursor'],$isnew);
		}
		return true;
	}
	public function up_member_mid($external_userid){
		if(!$_SESSION['uid'] || !$external_userid){
			return false;
		}
		pdo_update('jy_scrm_member',array('mid'=>$_SESSION['uid']),array('merchid'=>$_SESSION['onemerchid'],'external_userid'=>$external_userid,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function up_member_item($external_userid){
		$item=model('core/wxwork_userapi')->get_member_item($external_userid);
		$member=pdo_get('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'external_userid'=>$external_userid,'uniacid'=>$_SESSION['uniacid']),array('id','mid','name','avatar','mobile'));
		if(!$member['mid'] && $member['name']){
			if($this->plugin_exists('jy_worker_ewm')){
				$ewm_log=pdo_get('jy_worker_ewm_log',array('merchid'=>$_SESSION['onemerchid'],'nickname'=>$member['name'],'uniacid'=>$_SESSION['uniacid']),array('id','mid','createtime'));
				$createtime=$ewm_log['createtime'];
				if(time()-$createtime<600){
					$member['mid']=$ewm_log['mid'];
				}
			}
			if(!$member['mid'] && $member['mobile']){
		    	$member['mid']=pdo_getcolumn('core_members',array('realmobile'=>$member['mobile'],'uniacid'=>$_SESSION['uniacid']),'id');
		    	if(!$member['mid']){
    				$member['mid']=pdo_getcolumn('core_members',array('mobile'=>$member['mobile'],'uniacid'=>$_SESSION['uniacid']),'id');
    			}
			}
			if(!$member['mid']){
				$member['mid']=pdo_getcolumn('core_members',array('nickname'=>$member['name'],'uniacid'=>$_SESSION['uniacid']),'id');
			}
		}
		$item['id']=$member['id'];
		$item['mid']=$member['mid'];
		$item['name']=$item['name']?:$member['name'];
		$item['avatar']=$item['avatar']?:$member['avatar'];
		$item['mobile']=$item['mobile']?:$member['mobile'];
		$id=$this->create($item);
		return $id;
	}
	public function create($params){
		$id=$params['id'];
		if($params['follow_info']){
			$params['follow_user']=$params['follow_info'];
		}
		if($params['follow_user']['state']){
			if($params['follow_user']['add_way']=1 && $params['follow_user']['state']>=30){//二维码
				$params['codefrom']=$params['follow_user']['state']-30;
			}
		}
		$params['diyfields']=$params['diyfields']?:array();
		$mobile=$params['follow_user'] && $params['follow_user']['remark_mobiles']?$params['follow_user']['remark_mobiles'][0]:'';
		$tags=array();
		if($params['follow_user']['tags']){
			$tags=$params['follow_user']['tags'];
		}elseif($params['follow_user']['tag_id']){
			foreach($params['follow_user']['tag_id'] as $tv){
				$taginfo=pdo_get('jy_scrm_member_group_tag',array('tag_id'=>$tv,'uniacid'=>$_SESSION['uniacid']));
				$tags[]=array(
					'tag_name'=>$taginfo['title'],
					'tag_id'=>$tv,
					'userid'=>$params['follow_user']['userid'],
				);
			}
		}
		$member=array(
			'name'=>$params['name'],
			'avatar'=>$params['avatar'],
			'mobile'=>$params['mobile']?:$mobile,
			'position'=>$params['position'],
			'type'=>intval($params['type']),
			'gender'=>$params['gender'],
			'unionid'=>$params['unionid'],
			'corp_name'=>$params['corp_name'],
			'corp_full_name'=>$params['corp_full_name'],
			'external_profile'=>ijson_encode($params['external_profile']),
			'follow_user'=>ijson_encode($params['follow_user']),
			'follow_user_userid'=>$params['follow_user']['userid'],
			'follow_user_oper_userid'=>$params['follow_user']['oper_userid'],
			'remark'=>$params['remark'],
			'description'=>$params['description'],
			'comefrom'=>intval($params['follow_user']['add_way']),
			'codefrom'=>intval($params['codefrom']),
			'tags'=>ijson_encode($tags),
		);
		if($params['follow_user']['remark_company']){
			$member['remark_company']=$params['follow_user']['remark_company'];
		}
		if($params['follow_user']['remark_mobiles']){
			$member['remark_mobiles']=ijson_encode($params['follow_user']['remark_mobiles']);
		}
		if($params['follow_user']['remark_pic_mediaid']){
			$member['remark_pic_mediaid']=$params['follow_user']['remark_pic_mediaid'];
		}
		if($params['follow_user']['remark_pic']){
			$member['remark_pic']=$params['follow_user']['remark_pic'];
		}
		if($params['follow_user']['remark_pic']){
			$member['remark_pic']=$params['follow_user']['remark_pic'];
		}
		if($params['follow_user']['createtime']){
			$member['createtime']=$params['follow_user']['createtime'];
		}
		//unionid绑定粉丝
		if($params['unionid']){
			$member['mid']=$this->get_unionidmid($params['unionid']);
		}elseif($params['mid']){
			$member['mid']=$params['mid'];
		}
		if($id){
			$this->pdo_update('member',$member,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$member['workerid']=$this->model('worker')->get_workerid($member['follow_user_userid']?:$member['follow_user_oper_userid']);
			$member['external_userid']=$params['external_userid'];
			$member['uniacid']=$_SESSION['uniacid'];
			$member['merchid']=$_SESSION['onemerchid'];
			$member['createtime']=$member['createtime']?:TIMESTAMP;
			$this->pdo_insert('member',$member);
			$id=$this->pdo_insertid();
		}
		return $id;
	}
	public function get_unionidmid($unionid=0){
		$mid=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'unionid'=>$unionid),'id');
		return $mid;
	}
	public function up_member_remark($params){
		$params=array(
			'userid'=>$params['userid'],
			'external_userid'=>$params['external_userid'],
			'remark'=>$params['remark'],
			'description'=>$params['description'],
			'remark_company'=>$params['remark_company'],
			'remark_mobiles'=>$params['remark_mobiles'],
			'remark_pic_mediaid'=>$params['remark_pic_mediaid'],
		);
		model('core/wxwork_userapi')->set_member_remark($params);
		return true;
	}
	public function get_qunlist($userid='',$cursor='',$limit=100,$upitem=false){
		$content=model('core/wxwork_userapi')->get_qunlist($userid,$cursor,$limit);
		foreach($content['group_chat_list'] as $item){
			$id=pdo_getcolumn('jy_scrm_qun',array('chat_id'=>$item['chat_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($id){
				//	continue;
			}
			if(!$item['chat_id']){
				continue;
			}
			$quninfo=model('core/wxwork_userapi')->get_qunitem($item['chat_id']);
			$item=array_merge($item,$quninfo);
			$item['id']=$id;
			$this->create_qun($item,$upitem);
			$this->create_qunmember($item['member_list'],$item['chat_id']);
		}
		if($content['next_cursor']){
			$this->get_qunlist($userid,$content['next_cursor'],$limit);
		}
	}
	public function up_qun_item($chat_id){
		$item=model('core/wxwork_userapi')->get_qunitem($chat_id);
		$id=pdo_getcolumn('jy_scrm_qun',array('chat_id'=>$chat_id,'uniacid'=>$_SESSION['uniacid']),'id');
		$item['id']=$id;
		$id=$this->create_qun($item);
		return $id;
	}
	public function create_qun($params,$upitem=false){
		$qun=array(
			'title'=>$params['name']?:$params['title'],
			'desc'=>$params['desc'],
			'notice'=>$params['notice'],
			'owner'=>$params['owner'],
			'member_list'=>ijson_encode($params['member_list']),
			'num'=>count($params['member_list']),
			'status'=>intval($params['status']),
		);
		$qun['workerid']=$this->model('worker')->get_workerid($params['owner']);
		/*if($upitem){
			$qunitem=model('core/wxwork_userapi')->get_qunitem($params['chat_id']);
			$qun['title']=$qunitem['name'];
			$qun['owner']=$qunitem['owner'];
			$qun['desc']=$qunitem['desc'];
		}*/
		$id=$params['id'];
		if($id){
			$this->pdo_update('qun',$qun,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$id=$this->pdo_insertid();
		}else{
			$qun['chat_id']=$params['chat_id'];
			$qun['uniacid']=$_SESSION['uniacid'];
			$qun['merchid']=$_SESSION['onemerchid'];
			$qun['createtime']=$params['create_time']?:TIMESTAMP;
			$this->pdo_insert('qun',$qun);
		}
		return $id;
	}
	public function create_qunmember($member_list,$chat_id=''){
		if(!$chat_id){
			return false;	
		}
		foreach($member_list as $item){
			if(!$item['userid']){
				return false;	
			}
			$isqun=$this->pdo_get('qun_member',array('userid'=>$item['userid'],'chat_id'=>$chat_id,'uniacid'=>$_SESSION['uniacid']),'id');
			if($isqun['id']){
				continue;	
			}
			$member=array(
				'userid'=>$item['userid'],
				'unionid'=>$item['unionid'],
				'join_time'=>$item['join_time'],
				'join_scene'=>$item['join_scene'],
				'invitor'=>ijson_encode($item['invitor']),
				'invitor_userid'=>$item['invitor']['userid'],
				'group_nickname'=>$item['group_nickname'],
				'name'=>$item['name'],
				'type'=>$item['type'],
			);
			$id=$params['id'];
			if(!$isqun['id']){
				$member['chat_id']=$chat_id;
				$member['uniacid']=$_SESSION['uniacid'];
				$member['merchid']=$_SESSION['onemerchid'];
				$member['createtime']=TIMESTAMP;
				$this->pdo_insert('qun_member',$member);
			}
		}
		return true;
	}
	public function up_tags(){
		$this->model('model')->check_apilog('get_member_tags',true);
		$tags=model('core/wxwork_userapi')->get_member_tags();
		foreach($tags as $item){
			$groupid=pdo_getcolumn('jy_scrm_member_group',array('merchid'=>$_SESSION['onemerchid'],'group_id'=>$item['group_id'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$groupid){
				$group=array(
					'group_id'=>$item['group_id'],
					'title'=>$item['group_name'],
					'order'=>$item['order'],
					'status'=>$item['deleted']?1:0,
				);
				$group['uniacid']=$_SESSION['uniacid'];
				$group['merchid']=$_SESSION['onemerchid'];
				$group['createtime']=$item['create_time'];
				$this->pdo_insert('member_group',$group);
				$groupid=pdo_insertid();
			}
			foreach($item['tag'] as $tv){
					$tag=array(
						'title'=>$tv['name'],
						'groupid'=>$groupid,
					);
					$id=pdo_getcolumn('jy_scrm_member_group_tag',array('merchid'=>$_SESSION['onemerchid'],'tag_id'=>$tv['id'],'uniacid'=>$_SESSION['uniacid']),'id');
					if($id){
						$this->pdo_update('member_group_tag',$tag,array('merchid'=>$_SESSION['onemerchid'],'tag_id'=>$tv['id'],'uniacid'=>$_SESSION['uniacid']));	
					}else{
						$tag['uniacid']=$_SESSION['uniacid'];
						$tag['merchid']=$_SESSION['onemerchid'];
						$tag['tag_id']=$tv['id'];
						$tag['createtime']=$tv['create_time'];
						$this->pdo_insert('member_group_tag',$tag);
					}
			}
		}
		$this->model('model')->add_apilog('get_member_tags');
		return true;
	}
}
?>